package leetcode_字符串._07数字与字符串间转换;

import java.util.ArrayList;

/**
 * 分数加减运算
 * 算法 :
 *   先把字符串按照 '+' 拆分成数组
 *   这样数组的元素就是一个分数字符串
 *   遍历数组
 *   按照 '/' 拆分字符串
 *   创建两个 Fractions 对象 一个用来存放结果 另一个用来存放当前遍历的分数
 */
public class _592 {
    public static void main(String[] args) {
//        _592 test = new _592();
//        System.out.println(test.fractionAddition("5/3+1/3"));
        System.out.println(fractionAddition("-1/2+1/2"));
    }

    public static String fractionAddition(String expression) {
        if (expression.length() == 0) {
            return "";
        }
        Fractions ans = new Fractions(), tem = null;
        ArrayList<String> list = new ArrayList<>();
        StringBuilder sb = new StringBuilder();
        char[] chars = expression.toCharArray();
        for (int i = 0; i < chars.length; ++i) {
            if (chars[i] == '+' || (chars[i] == '-' && i != 0)) {
                list.add(sb.toString());
                sb.delete(0, sb.toString().length());
                if (chars[i] == '+') {
                    continue;
                }
            }
            // 如果是减号的话，还要再加上去
            sb.append(chars[i]);
            if (i == chars.length - 1) {
                list.add(sb.toString());
            }
        }
        for (int i = 0; i < list.size(); i++) {
            String[] strings = list.get(i).split("/");
            int a = Integer.parseInt(strings[0]), b = Integer.parseInt(strings[1]);
            if (i == 0) {
                ans.molecular = a;
                ans.denominator = b;
                continue;
            }
            tem = new Fractions(a, b);
            ans.add(tem);
            ans.simplify();
            System.out.println("ans --->" + ans.toString());
        }
        return ans.molecular + "/" + ans.denominator;
    }

    static class Fractions {
        int molecular;
        int denominator;

        public Fractions() {}

        public Fractions(int molecular, int denominator) {
            this.molecular = molecular;
            this.denominator = denominator;
        }

        /**
         * 分数加减 , 相减就是负数相加
         * 把分母转化成相同的
         * 求它们的最小公倍数
         * a 和 b 的最小公倍数就是都能够整除 a 和 b , 并且是最小的
         * 最小公倍数算法 :
         *   两个数的乘积除以两个数的最大公约数
         *
         * @param fractions
         * @return
         */
        public Fractions add(Fractions fractions) {
            System.out.println("add --->" + fractions.toString());
            if (this.denominator == fractions.denominator) {
                this.molecular = this.molecular + fractions.molecular;
                System.out.println("==_this --->" + this.toString());
            } else {
                this.molecular = this.molecular * fractions.denominator + fractions.molecular * this.denominator;
                this.denominator = this.denominator * fractions.denominator;
                System.out.println("!=_this --->" + this.toString());
            }
            return this;
        }

        /**
         * 化简
         * 求分子分母的最大公约数
         * 辗转相除法
         * 计算 a 和 b 的最大公约数 , 假设 a > b
         * c = a % b
         * c != 0 -> b % c
         */
        public void simplify() {
            int a = Math.max(this.molecular, this.denominator),  b = Math.min(this.molecular, this.denominator);
            while (b != 0) {
                int n = a % b;
                a = b;
                b = n;
            }
            this.molecular = this.molecular / a;
            this.denominator = this.denominator / a;
            if (this.denominator < 0 && this.molecular > 0) {
                this.denominator = -this.denominator;
                this.molecular = -this.molecular;
            }
        }

        @Override
        public String toString() {
            return "Fractions{" +
                    "molecular=" + molecular +
                    ", denominator=" + denominator +
                    '}';
        }
    }
}
